
function getUtils() {
  return {
    request: window.qiBao?.$request,
    store: window.qiBao?.$store,
    getAppId: window.qiBao?.$utils.getAppId
  }
}

// 获取页面配置信息
export function getPageInfo(query) {
  console.log(getUtils())
  return getUtils().request({
    url: '/page/getPageInfo',
    method: 'get',
    params: query
  })
}

// 获取子页面
export function getNavByPageId(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/init/getNavByPageId',
    method: 'post',
    data: data
  })
}

export function getClientMacro() {
  const macro = getUtils().store.getters['page/clientMacro']
  if (getUtils().getAppId()) {
    macro['appId'] = getUtils().getAppId()
  }
  const clientMacro = {
    clientMacro: JSON.stringify(macro)
  }
  return clientMacro
}
window.getClientMacro = getClientMacro
// 获取左侧区域数据
export function getLeftData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/page/getLeftData',
    method: 'post',
    data: data
  })
}

// 获取主列表数据
export function getMainData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/page/getMainData',
    method: 'post',
    data: data
  })
}

// 通用接口-单条数据
export function commonInvoke(data, clientMacro = false) {
  let newData = JSON.parse(JSON.stringify(data))
  const appId = getUtils().getAppId() || ''
  newData = Object.assign({
    appId
  }, newData)
  if (clientMacro) {
    const clientMacro = getClientMacro()
    newData = Object.assign({}, clientMacro, newData)
  }
  delete newData.data.REFDATA
  delete newData.data.refdata
  Object.keys(newData.data).forEach((key) => {
    if (key.startsWith('REFDATA.') || key.startsWith('refdata.')) {
      delete newData.data[key]
    }
  })
  return getUtils().request({
    url: '/invoke/func',
    method: 'post',
    data: newData,
    meta: {
      loading: true // 是否在接口请求时添加loading
    }
  })
}

// 通用接口-多条数据
export function multiCommonInvoke(data, clientMacro = false) {
  let newData = JSON.parse(JSON.stringify(data))
  newData.dataset.forEach(item => {
    delete item.REFDATA
    delete item.refdata
    Object.keys(item).forEach((key) => {
      if (key.startsWith('REFDATA.') || key.startsWith('refdata.')) {
        delete item[key]
      }
    })
  })
  if (clientMacro) {
    const clientMacro = getClientMacro()
    newData = Object.assign({}, clientMacro, newData)
  }
  return getUtils().request({
    url: '/invoke/funcMoreData',
    method: 'post',
    data: newData,
    meta: {
      loading: true
    }
  })
}

// 新增创建表单【废弃】
export function getAddFormInfo(data) {
  return getUtils().request({
    url: '/form/add',
    method: 'post',
    data: data
  })
}

// 获取详情数据【废弃】
export function getEditFormInfo(data) {
  return getUtils().request({
    url: '/form/getEditData',
    method: 'post',
    data: data
  })
}

// 获取表单详情
export function getFormData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/form/getFormData',
    method: 'post',
    data: data
  })
}

// 获取表单详情
export function saveFormData(data) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data))
  delete newData.data.REFDATA
  return getUtils().request({
    url: '/form/saveFormData',
    method: 'post',
    data: newData,
    meta: {
      loading: true
    }
  })
}

// 获取表单详情-手工
export function getFormDataFromId(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/form/getFormDataByFormId',
    method: 'post',
    data: data
  })
}

// 获取表单详情
export function saveFormDataByFormId(data) {
  // 删除REFDATA字段
  const newData = JSON.parse(JSON.stringify(data))
  delete newData.data.REFDATA
  return getUtils().request({
    url: '/form/saveFormDataByFormId',
    method: 'post',
    data: newData,
    meta: {
      loading: true
    }
  })
}

// 获取菜单详情
export function invokeMethod(data) {
  const newData = Object.assign({
    pageId: window.$CD?.pageId || '',
    appId: getUtils().getAppId() || ''
  }, data)
  return getUtils().request({
    url: '/invoke/method',
    method: 'post',
    data: newData
  })
}

// 多数据请求接口
export function invokeMethodMore(data) {
  const newData = Object.assign({
    pageId: window.$CD?.pageId || '',
    appId: getUtils().getAppId() || ''
  }, data)
  return getUtils().request({
    url: '/invoke/methodMoreData',
    method: 'post',
    data: newData
  })
}

// 获取通用数据
export function getDataSource(data) {
  const newData = Object.assign({
    pageId: window.$CD?.pageId,
    appId: getUtils().getAppId() || ''
  }, data)
  return getUtils().request({
    url: '/invoke/datasource',
    method: 'post',
    data: newData
  })
}

// 获取全部码表
export function getCodeData(data) {
  return getUtils().request({
    url: '/init/getCodeData',
    method: 'get',
    params: data
  })
}

// 获取lookup数据
export function getColLookupData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/form/getColLookupData',
    method: 'post',
    data: data
  })
}

// 获取系统服务数据
export function getColServiceData(data) {
  return getUtils().request({
    url: '/form/getColServiceData',
    method: 'post',
    data: data
  })
}

// 文件上传
export function fileUpload(data, file) {
  const form = new FormData()
  Object.keys(data).forEach(key => {
    form.append(key, data[key])
  })
  form.append('file', file)
  return getUtils().request({
    url: '/file/upload',
    method: 'post',
    data: form
  })
}

// 文件删除
export function fileDelete(params) {
  return getUtils().request({
    url: '/file/delete',
    method: 'get',
    params
  })
}

// 保存排序
export function saveOrderId(data) {
  return getUtils().request({
    url: '/form/saveOrderId',
    method: 'post',
    data: data,
    meta: {
      loading: true
    }
  })
}

// 保存自定义字段
export function saveDisplayList(data) {
  return getUtils().request({
    url: '/page/saveDisplayList',
    method: 'post',
    data
  })
}

// 保存自定义列宽
export function saveColumnWidth(data) {
  return getUtils().request({
    url: '/page/saveColumnWidth',
    method: 'post',
    data,
    meta: {
      globalWarn: false // 忽略报错
    }
  })
}

// 获取宏变量
export function getMacro(data) {
  const clientMacro = getClientMacro()
  return clientMacro
}

// 导出
export function tableExport(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/Export/download',
    method: 'post',
    data
  })
}

// 检测字段值是否唯一
export function checkColUnique(data) {
  return getUtils().request({
    url: '/form/checkColUnique',
    method: 'post',
    data
  })
}

// 复制-不入库
export function getCopyData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/form/getCopyData',
    method: 'post',
    data
  })
}

// 统计
export function getStatisticInfoData(data) {
  const clientMacro = getClientMacro()
  data = Object.assign({}, clientMacro, data)
  return getUtils().request({
    url: '/page/getStatisticInfoData',
    method: 'post',
    data
  })
}

// 获取id
export function getOneId(query) {
  return getUtils().request({
    url: '/invoke/getOneId',
    method: 'get',
    params: query
  })
}

// 获取项目变更人员
export function getEmployeeList(params) {
  return getUtils().request({
    url: '/connections/GetEmployeeList',
    method: 'get',
    params
  })
}

// 获取通用数据
export function getExtendEleDisplayList(params) {
  return getUtils().request({
    url: '/page/getExtendEleDisplayList',
    method: 'get',
    params
  })
}

// 获取列关联页面
export function getColRefPageInfo(params) {
  const clientMacro = getClientMacro()
  const data = Object.assign({}, clientMacro)
  return getUtils().request({
    url: '/form/getColRefPageInfo',
    method: 'get',
    params,
    data
  })
}

// 主表导入
export function importMainExcel(data, file) {
  const form = new FormData()
  Object.keys(data).forEach(key => {
    form.append(key, data[key])
  })
  form.append('file', file)
  return getUtils().request({
    url: '/import/importMainTableData',
    method: 'post',
    data: form
  })
}

// 子表导入
export function importSubExcel(data, file) {
  const form = new FormData()
  Object.keys(data).forEach(key => {
    form.append(key, data[key])
  })
  form.append('file', file)
  return getUtils().request({
    url: '/import/importSubTableData',
    method: 'post',
    data: form
  })
}
